import { useTable, LIST_TYPE } from '@crud/index';
import { getColonelUserlist } from '@/api/crm-agency/dyj/colonel';
import { formatParamsSelectVal } from '@/utils';
import { useRoute } from 'vue-router';

// 查询
export const codeTypeOptions = [
  { label: '全部', value: -1 },
  { label: '老客户', value: 0 },
  { label: '新客户', value: 1 }
];

// 表格
export const useTableList = (options: any = {}) => {
  const route = useRoute();

  const columns: any = [
    { label: '客户昵称', prop: 'name', minWidth: 120, fixed: 'left' },
    { label: '手机号', prop: 'mobile', minWidth: 160 },
    { label: '客户类型', prop: 'is_new_order', minWidth: 120, render: (s: any) => (s.row.is_new_order === 1 ? '新用户' : '老用户') },
    { label: '会员到期时间', prop: 'end_time', minWidth: 160, type: 'date' },
    { label: '关联时间', prop: 'complete_time', minWidth: 160, type: 'date' },
    { label: '购买次数', prop: 'buy_count', minWidth: 120 },
    { label: '备注', prop: 'remark', minWidth: 200, render: 'remark', align: 'center' }
  ];

  return useTable({
    ...options,
    type: LIST_TYPE.AGENCY,
    params: {
      key_word: '',
      gen_code: '',
      user_type: -1,
      product: -1,
      code_status: -1,
      from_dt: undefined,
      to_dt: undefined,
      complete_from_dt: undefined,
      complete_to_dt: undefined,
      colonel_id: undefined
    },
    columns: columns,
    getParams: ({ _timeArr = [], _completeTimeArr = [], ...params }) => {
      // 处理时间参数
      if (_timeArr?.length) {
        const [begin, end] = _timeArr;
        params.from_dt = begin;
        params.to_dt = end;
      }
      if (_completeTimeArr?.length) {
        const [begin, end] = _timeArr;
        params.complete_from_dt = begin;
        params.complete_to_dt = end;
      }

      params.colonel_id = route.query.colonelId;
      return {
        ...params,
        ...formatParamsSelectVal([['user_type', -1]], params)
      };
    },
    getList: getColonelUserlist
  });
};
